/*
 * Copyright (c) 2011-2012 Alexander Dubu
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * o  Redistributions of source code must retain the above copyright notice,
 *    this list of conditions and the following disclaimer.
 *
 * o  Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution.
 *
 * o  Neither the name Axil nor the names of its contributors may be used to
 *    endorse or promote products derived from this software without specific
 *    prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */
package axil.framework.error;

import axil.api.Argument;
import axil.api.Formatted;
import axil.api.Formatting;
import axil.api.Persona;
import axil.api.error.AxilEvaluationException;
import axil.api.error.Frame;
import axil.api.extend.Source;
import axil.framework.localization.Message;
import axil.framework.localization.MessageContainer;
import axil.vm.stack.StackFrame;

import java.util.List;

import static axil.framework.Functions.*;


/**
 * An implementation of the AxilEvaluationException class.
 */
public class EvaluationException extends AxilEvaluationException
	implements Formatted, MessageContainer {
	private StackFrame stack;
	private Message message;


	public EvaluationException(Throwable e, StackFrame stack, Message message) {
		super(e);
		this.message = message;
		this.stack = stack;
	}


	public EvaluationException(StackFrame stack, Message message) {
		this.message = message;
		this.stack = stack;
	}


	public EvaluationException(Throwable e, Source source, Message message) {
		super(e);
		this.message = message;
		this.stack = StackFrame.create(null, source, new int[0]);
	}


	/**
	 * Get the translation key. The translation key is the key used to look up
	 * the actual translation text. This key may contain '-' characters but
	 * will not contain any whitespace or other punctuation.
	 *
	 * @return
	 * 	Returns the translation key, which is never null or empty.
	 */
	public String key() {
		return message.key();
	}


	/**
	 * Get the optional message values. Values are name/value pairs containing a
	 * key and the raw, unformatted value.
	 *
	 * @return
	 * 	Returns the message values. The array is never null, but may be empty.
	 */
	public Argument[] values() {
		return message.values();
	}


	/**
	 * Get the translatable message contained within this object. The object
	 * returned is never null.
	 */
	public Message message() {
		return message;
	}


	/**
	 * The stack trace for the script. The stack frames are listed in order from
	 * the starting point to the point at which execution halted.
	 *
	 * @return
	 * 	Returns the set of stack frames. The list returned may be empty if the
	 * 	execution issue occurred outside of the script source code.
	 */
	public List<Frame> stack() {
		return stack.stack();
	}


	/**
	 * Returns the detail message string of this throwable.
	 */
	public String getMessage() {
		return getLocalizedMessage();
	}


	/**
	 * Creates a localized description of this throwable.
	 */
	public String getLocalizedMessage() {
		return fmt(this);
	}


	/**
	 * Format this object in a locale-friendly manner. The string returned is
	 * a localized representation of this object using the locale and timezone
	 * associated with this persona. The formatting given is never null and
	 * matches formatting suitable for this type.
	 */
	public String format(Persona persona, Formatting formatting) {
		return message.format(persona, formatting);
	}


	/**
	 * Return a string representation of this object. The string returned is
	 * never null.
	 */
	public String toString() {
		return single(wrap(getLocalizedMessage()));
	}
}
